<?php
class GoodsAction extends CommonAction{
	/**
	 * 显示商品列表
	 */
	public function index(){
		import('ORG.Util.Page');
		$db=M('goods');
		$count=$db->count('id');
		$page=new Page($count,10);
		$limit = $page->firstRow . ',' . $page->listRows;
		$field=array('id','name','listpic','sprice','number');
		$goods = $db->field($field)->limit($limit)->select();
		$this->data=$goods;
		$this->page=$page->show();
		$this->display();
	}
	/**
	 * 显示商品添加模板
	 */
	public function addGoods(){
		$db=M('category');
		$brand=M('brand');
		$data=$db->select();
		$cate=recursion($data,0,2);
		$this->cate=$cate;
		$this->brand=$brand->select();
		$this->display();
	}
	/**
	 * 异步获取属性与规格
	 */
	public function getAttr(){
		$tid=intval($_POST['tid']);
		$db=M('type_attr');
		$data=$db->where(array('tid'=>$tid))->select();
		echo json_encode($data);
	}
	/**
	 * 商品添加处理
	 */
	public function runAddGoods(){
		$data=array(  //不报错的话，最可能是字段有错误
			'name'=>htmlspecialchars($_POST['name']),
			'mprice'=>(float)$_POST['mprice'],
			'sprice'=>(float)($_POST['sprice']),
			'unit'=>htmlspecialchars($_POST['unit']),
			'inventory'=>$_POST['inventory'],
			'click'=>intval($_POST['click']),
			'number'=>htmlspecialchars($_POST['number']),
			'time'=>time(),
			'listpic'=>$_POST['listpic'],
			'bid'=>empty($_POST['bid'])?0 :(int)$_POST['bid'],
			'aid'=>(int)session('uid'),
			'cid'=>(int)$_POST['cid'],
			'tid'=>(int)$_POST['tid']
				);
		$intro=array(
			'max'=>implode('|',$_POST['max']),
			'medium'=>implode('|',$_POST['medium']),
			'mini'=>implode('|',$_POST['mini']),
			'intro'=>$_POST['intro'],
			'service'=>$_POST['service']
			);
		/*p($intro);
		die;*/
		$attr=$_POST['attr'];
		$spec=$_POST['spec'];
		$goodsAttr=array();
		foreach($attr as $k=>$v){
			$goodsAttr[]=array(
				'aid'=>intval($k),
				'value'=>htmlspecialchars($v)
				);
		}
		foreach($spec as $k=>$v){
			for($i=0;$i<count($v['value']);$i++){
				$goodsAttr[]=array(
					'aid'=>$k,
					'value'=>$v['value'][$i],
					'price'=>(float)$v['price'][$i]
					);
			}
		}
		$data['intro']=$intro;
		$data['goods_attr']=$goodsAttr;
		/*p($data);
		die;*/
		if (D('GoodsRelation')->relation(true)->add($data)) {
			$this->success('添加商品成功',U('index'));
		}else{
			$this->error('添加失败');
		}
	}
	/**
	 * 商品修改处理
	 */
	public function updateGoods(){
		$db=M('category');
		$brand=M('brand');
		$data=$db->select();
		$cate=recursion($data,0,2);
		$this->cate=$cate;
		$this->brand=$brand->select();
		$id=(int)($_GET['id']);
		$datas=D('GoodsRelation')->relation(true)->where(array('id'=>$id))->find();
		$type=D('GoodsAttrView');
		$result=$type->where(array('gid'=>$id))->select();
		//echo $type->getlastsql();
		//p($result);
		//$arr=array();
		foreach($result as $k=>$v){
			$result[$k]['tvalue']=explode(',',$v['tvalue']);
		}
		//p($arr);
		p($result);
		$this->result=$result;
		$this->datas=$datas;
		$this->display();
	}
	/**
	 * 删除商品处理
	 */
	public function delGoods(){
		$id=intval($_GET['id']);
		$arr=D('GoodsView')->where(array('id'=>$id))->find();//查找图片
		$data=D('GoodsDelRelation')->relation(true)->delete($id);//删除数据
		unlink('./Uploads/List/'.$arr['listpic']);
		$mini=explode('|',$arr['mini']);
		$max=explode('|',$arr['max']);
		$medium=explode('|',$arr['medium']);
		$pic=array();
		$pic=array_merge($max,$medium,$mini);
		foreach($pic as $v){
			unlink('./Uploads/Picture/'.$v); //删除图册
		}
		$intro=$arr['intro'];
		$service=$arr['service'];
		$preg='/Uploads.*?(?=")/is';
	 	//$pre=ltrim(__ROOT__,'/');
		//$preg="/".$pre."\/(.*?)\"/is";
		preg_match_all($preg,$intro,$intro_pic);
		preg_match_all($preg,$service,$service_pic);
		$int_ser=array();
		$int_ser=array_merge($intro_pic,$service_pic);
		foreach($int_ser as $v){  //删除说明图跟售后服务图
			foreach($v as $value){
				unlink('./'.$value);
			}
		}
		/*p($int_ser);
		die;*/
		if($data){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}
	/**
	 * 显示添加货品模板
	 */
	public function item(){
		$db=M('goods_attr');
		$gid=(int)$_GET['gid'];
		$where=array('gid'=>$gid);
		$field=array('aid');
		$data=$db->where($where)->field($field)->group('aid')->select();
		foreach($data as $k=>$v){
			$arr[$k]=$v['aid'];
		}
		$attr=M('type_attr');
		$wh=array('id'=>array('IN',$arr),'type'=>1);//IN多个获得
			$typeName=$attr->where($wh)->field(array('id','name'))->select();//找出这个商品的规格
		//将￥typeName分配过去
			/*p($typeName);
			die;*/
			$spec=array();
			foreach($typeName as $v){
				$where=array('aid'=>$v['id'],'gid'=>$gid); //条件是商品的id和规格的id
				$spec[]=$db->where($where)->field(array('id','value'))->select();//用于添加货品的规格（该商品用于的部分规格）
			}
			/*p($typeName);
			die;*/
			$where=array('gid'=>$gid);//显示这一商品的货品
			$list=M('goods_list')->where($where)->select();//good_list的value属性保存的是商品属性的id
			foreach($list as $k=>$v){
				$array=array();
				$arr=explode('|',$v['attr']);
				$where=array('id'=>array("IN",$arr));
				$list[$k]['attr']=$db->where($where)->field(array('value'))->select();
				/*foreach($list[$k]['attr'] as $value){
					$array[]=$value['value'];
				}
				$list[$k]['attr']=implode(',',$array);*/
			}
			/*p($list);
			die;*/
		$this->list=$list;
		$this->typeName=$typeName;
		$this->spec=$spec;
		$this->display();
	}
	/**
	 * 货品添加处理
	 */
	public function addItem(){
		$gid=(int)$_POST['gid'];
		$attr=$_POST['attr']; //货品属性的id
		$attr=implode('|',$attr);
		$inventory=intval($_POST['inventory']);
		$number=htmlspecialchars($_POST['number']);
		$data=array(
			'gid'=>$gid,
			'attr'=>$attr,
			'inventory'=>$inventory,
			'number'=>$number
			);
		/*p($data);
		die;*/
		$db=M('goods_list');
		$id=$db->data($data)->add();
		if($id){
			$this->success('添加成功');
		}else{
			$this->error('添加失败');
		}
	}
	/**
	 * 货品删除处理
	 */
	public function delItem(){
		$id=intval($_GET['id']);
		$db=M('goods_list');
		$rows=$db->where(array('id'=>$id))->delete();
		if($rows){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}
	/**
	 * 货品修改模板
	 */
	public function updateItem(){
		$id=intval($_GET['id']);
		$db=M('goods_list');
		$data=$db->where(array('id'=>$id))->field(array('id','attr','inventory','number','gid'))->find();
		$attr_id=explode('|',$data['attr']);//货品属性的id
		$goodsAttr=M('goods_attr');
		$attr=$goodsAttr->where(array('id'=>array("IN",$attr_id)))->field(array('aid'))->select();
		$arr=array();
		foreach($attr as $k=>$v){
			$arr[$k]=$v['aid'];
		}
		$typeAttr=M('type_attr');
		$typeName=$typeAttr->where(array('id'=>array('IN',$arr)))->field('id','name')->select();//获得规格的名称，红色的名称是颜色，X XXｌ的名称是尺寸
		//$array=$goodsAttr->where(array('aid'=>array('IN',$arr),'gid'=>$data['gid']))->field(array('id','value'))->select();//获得这类商品的规格
		$array=array();
		foreach($arr as $v){
			$array[]=$goodsAttr->where(array('aid'=>$v,'gid'=>$data['gid']))->field(array('id','value'))->select();
		}
		/*p($array);
		die;*/
		$this->typeName=$typeName;
		$this->attr_id=$attr_id;
		$this->data=$data;
		$this->goodsAttr=$array;
		$this->display();
	}
	/**
	 * 货品修改处理
	 */
	public function runUpdateItem(){
		$gid=intval($_POST['gid']);
		$id=intval($_POST['id']);
		$attr=$_POST['attr'];
		$number=htmlspecialchars($_POST['number']);
		$inventory=(int)$_POST['inventory'];
		$attr=implode('|',$attr);
		$data=array(
			'number'=>$number,
			'inventory'=>$inventory,
			'attr'=>$attr
			);
		$db=M('goods_list');
		$rows=$db->where(array('id'=>$id))->data($data)->save();
		if($rows){
			$this->success('修改成功',U('item',array('id'=>$id,'gid'=>$gid)));
		}else{
			$this->error('修改失败');
		}
	}
}
?>